package com.ruixiang.study;

import org.junit.Test;

import java.io.*;

/**
 * @author ruixinag
 * @description
 * @create Created in 2022/1/19 23:53
 */
public class Demo4 {


    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\MSI\\Desktop\\sqli-labs-master");
        getFile(file);
    }
    private static void getFile(File file) throws IOException {
        File[] files = file.listFiles();

        //
        for (int i = 0; i < files.length; i++) {
            // isFile
            File file1 = files[i];
            String line = null;
            if (file1.isDirectory()) {
                // 目录
                getFile(file1);
            } else if (file1.isFile()) {
                // 文件
                String path = file1.getAbsolutePath();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "utf-8"));

                String newPath=path+"1";
                File newFile=new File(newPath);
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile,true)));
                while ((line = bufferedReader.readLine()) != null) {

                    line = line.replace("mysql_connect", "mysqli_connect");
                    line = line.replace("mysql_error()", "mysqli_error($con)");
                    line = line.replace("mysql_connect_errno", "mysqli_connect_errno()");
                    line = line.replace("mysql_select_db($dbname1,$con)", "mysqli_select_db($con,$dbname1)");
                    line = line.replace("mysql_select_db($dbname,$con)", "mysqli_select_db($con,$dbname)");
                    line = line.replace("mysql_fetch_array", "mysqli_fetch_array");
                    line = line.replace("mysql_query($sql)", "mysqli_query($con,$sql)");

                    writer.write(line);
                    writer.newLine();
                }

                bufferedReader.close();
                writer.close();

                String name = file1.getPath();
                file1.delete();
                newFile.renameTo(new File(name));


            }
        }
    }



}
